/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
 * main.c
 *
 * Copyright (C) 2008, 2009 Lingtao Pan
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, 
 * Boston, MA 02111-1307, USA.
 */

#include "common.h"

struct Viewer *left_viewer, *right_viewer;
struct GftpInterface *gftp_interface;
GtkUIManager *ui_manager;

pthread_t main_thread_id;

pthread_mutex_t log_mutex = PTHREAD_MUTEX_INITIALIZER;



static void openurl_on_startup (const char *url) 
{
    right_viewer->request = gftp_request_new (ftp_log_gui);
    if (gftp_request_parse_url (right_viewer->request, url) < 0) {
        g_debug ("openurl_on_startup parse url fails");
        return;
    }

    gtk_combo_box_append_text (GTK_COMBO_BOX(gftp_interface->hostedit),
                               right_viewer->request->hostname);
    gtk_entry_set_text (GTK_ENTRY(gtk_bin_get_child
                                  (GTK_BIN (gftp_interface->hostedit))),
                        right_viewer->request->hostname);

    if (right_viewer->request->username != NULL) {
        gtk_combo_box_append_text (GTK_COMBO_BOX(gftp_interface->useredit),
                                   right_viewer->request->username);
        gtk_entry_set_text (GTK_ENTRY(gtk_bin_get_child
                                      (GTK_BIN (gftp_interface->useredit))),
                            right_viewer->request->username);
    }

    on_viewer_connecting (right_viewer);
    enable_progressbar (gftp_interface);
    gftpui_generic_thread (perform_connection, NULL);
    disable_progressbar (gftp_interface);

    if (gftp_request_is_connected (right_viewer->request)) {
        gftp_viewer_combo_set_text_no_emission (right_viewer, 
                                  right_viewer->request->directory);
        gftp_viewer_show_files (right_viewer);
        on_viewer_connected (right_viewer);
    }

}

static void
setup_left_viewer ()
{ 
    left_viewer->request = gftp_request_new (ftp_log_gui);
    left_viewer->tid = main_thread_id;
    gchar *sdir;
    gchar *tempstr;

    if (gftp_protocols[GFTP_LOCAL_NUM].init (left_viewer->request) == 0) {
        sdir = gftp_config_get_string ("General", "local_startup_directory");
        if (sdir == NULL || strlen (g_strstrip (sdir)) == 0) {
            g_free (sdir);
            sdir = g_strdup ("~");     /* the following for g_free(sdir) */
        }

        tempstr = gftp_expand_path (sdir);
        gftp_request_set_directory (left_viewer->request, tempstr);
        gftp_viewer_get_file_list (left_viewer);
        gftp_viewer_show_files (left_viewer);

        gftp_viewer_combo_set_text_no_emission (left_viewer, 
                                    left_viewer->request->directory);
        on_viewer_connected (left_viewer);

        g_free (sdir);
        g_free (tempstr);
    }
}


static void feature_test ()
{
    gchar *str;
    off_t size;
    int i;
    gftp_file *fle = g_malloc0 (sizeof(gftp_file));
    
    size = 6180064;

    /* str = g_strdup_printf ("%ld", size); */
    
    /* puts (str); */

    i = sizeof (off_t);
    
    printf ("length of off_t is %d\n", i);
    printf ("length of long int is %d\n", sizeof(long int));
    size = 2466111999;
    
    printf ("file size if " GFTP_OFF_T_PRINTF_MOD " \n", size);
    gftp_info ();   

}

int
main (int argc, char **argv)
{
    /* feature_test (); */

#ifdef ENABLE_NLS                               
    setlocale (LC_ALL, "");

    bindtextdomain(PACKAGE, PACKAGE_LOCALE_DIR);
    bind_textdomain_codeset(PACKAGE, "UTF-8");
    textdomain(PACKAGE);
#endif                  

 
    g_thread_init (NULL);
    gdk_threads_init ();
    gdk_threads_enter ();

    gftp_config_load ();
    gftp2_bookmark_load ();

    main_thread_id = pthread_self ();

    gtk_init (&argc, &argv);

    gtk_icon_theme_prepend_search_path (gtk_icon_theme_get_default (),
                                        PKGDATADIR);

    gftp_interface = gftp_interface_new ();
    setup_left_viewer ();

    g_timeout_add (1000, transfer_view_update_download_status, NULL);

    /* parse options */
    if (argc == 2) {
        openurl_on_startup (argv[1]);
    }
  
    gtk_main ();  
    gdk_threads_leave ();  

    return (0);
}


